package com.zmn.mcc.admin.controller.organize;

import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.dictionary.GlobalDict;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.collection.CollectionUtil;
import com.zmn.common.utils.number.NumberUtil;
import com.zmn.mcc.model.entity.module.McModule;
import com.zmn.mcc.model.entity.organize.McOrganize;
import com.zmn.mcc.model.entity.organize.McOrganizeQuery;
import com.zmn.mcc.model.entity.staff.McStaff;
import com.zmn.mcc.services.interfaces.staff.StaffService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author heguanghua
 * @since 2020/04/15 10:28
 */
@Controller
@RequestMapping("/organize/")
public class OrganizeListController extends OrganizeBaseController {
    @Resource
    private StaffService staffService;

    /**
     * 组织列表
     */
    @RequestMapping(value = "list")
    public String list(HttpServletRequest request,
                       @ModelAttribute McOrganizeQuery query,
                       Model model) {

        String keyword = request.getParameter("keyword");
        if (NumberUtil.isNumber(keyword)) {
            query.setOrgId(Integer.parseInt(keyword));
            query.setOrgName("");
        } else {
            query.setOrgName(keyword);
        }

        List<McOrganize> items = organizeService.listPageByQuery(query);

        model.addAttribute("items", items);
        model.addAttribute("vo", query);

        model.addAttribute("keyword", keyword);

        return "organize/list";
    }

    /**
     * 组织树状列表
     */
    @RequestMapping("tree")
    public String config(HttpServletRequest request,
                         @ModelAttribute McOrganizeQuery query,
                         Model model) {

        McOrganize organize = NumberUtil.isNotNullOrZero(query.getParentId()) ? organizeService.findByKey(query.getParentId()) : new McOrganize();
        McOrganize parent = new McOrganize();
        if(NumberUtil.isNotNullOrZero(organize.getParentId()) && organize.getParentId() > GlobalConsts.TOP_ID) {
            parent = organizeService.findByKey(organize.getParentId());
        }

        model.addAttribute("organize", organize);
        model.addAttribute("parent", parent);
        model.addAttribute("vo", query);

        return "organize/list_tree";
    }
    @RequestMapping("ajaxTree")
    @ResponseBody
    public ResponseDTO ajaxTree(HttpServletRequest request, HttpServletResponse response,
                                @ModelAttribute McOrganizeQuery query) {

        return ResponseDTO.success(organizeBService.listOrganize(query));
    }

    /**
     * 组织详情
     */
    @RequestMapping("detail")
    public String detail(HttpServletRequest request,
                         @RequestParam(value = "orgId", defaultValue = "0") Integer orgId,
                         @RequestParam(value = "parentId", defaultValue = "0") Integer parentId,
                         Model model) {

        McOrganize obj = !NumberUtil.isNullOrZero(orgId) ? organizeService.findByKey(orgId) : new McOrganize();
        McOrganize parent = new McOrganize();
        McStaff leader = new McStaff();
        McStaff manager = new McStaff();
        if(NumberUtil.isNullOrZero(orgId) && NumberUtil.isNotNullOrZero(parentId)) {
            obj.setParentId(parentId);
        }
        if(obj.getParentId() != null && obj.getParentId() > GlobalConsts.TOP_ID) {
            parent = organizeService.findByKey(obj.getParentId());
        }
        if(NumberUtil.isNullOrZero(obj.getParentId())) {
            obj.setParentId(GlobalConsts.TOP_ID);
            parent.setOrgId(GlobalConsts.TOP_ID);
        }
        if (NumberUtil.isNotNullOrZero(obj.getLeaderId())) {
            leader = staffService.findByKey(obj.getLeaderId());
        }
        if (NumberUtil.isNotNullOrZero(obj.getManagerId())) {
            manager = staffService.findByKey(obj.getManagerId());
        }

        model.addAttribute("obj", obj);
        model.addAttribute("parent", parent);
        model.addAttribute("leader", leader);
        model.addAttribute("manager", manager);

        model.addAttribute("plats", GlobalDict.getPlatTypeList());

        return "organize/detail";
    }
}
